package com.ruoyi.project.mydy.movieimage.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.mydy.movieimage.domain.DlMovieImage;
import com.ruoyi.project.mydy.movieimage.service.IDlMovieImageService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 影片图集Controller
 * 
 * @author mhg
 * @date 2021-10-07
 */
@Controller
@RequestMapping("/mydy/movieimage")
public class DlMovieImageController extends BaseController
{
    private String prefix = "mydy/movieimage";

    @Autowired
    private IDlMovieImageService dlMovieImageService;

    @Value("${my_upload_path}")
    private String dirPath;

    @RequiresPermissions("mydy:movieimage:view")
    @GetMapping()
    public String movieimage()
    {
        return prefix + "/movieimage";
    }

    /**
     * 查询影片图集列表
     */
    @RequiresPermissions("mydy:movieimage:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DlMovieImage dlMovieImage)
    {
        startPage();
        List<DlMovieImage> list = dlMovieImageService.selectDlMovieImageList(dlMovieImage);
        return getDataTable(list);
    }

    /**
     * 导出影片图集列表
     */
    @RequiresPermissions("mydy:movieimage:export")
    @Log(title = "影片图集", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DlMovieImage dlMovieImage)
    {
        List<DlMovieImage> list = dlMovieImageService.selectDlMovieImageList(dlMovieImage);
        ExcelUtil<DlMovieImage> util = new ExcelUtil<DlMovieImage>(DlMovieImage.class);
        return util.exportExcel(list, "影片图集数据");
    }

    /**
     * 新增影片图集
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存影片图集
     */
    @RequiresPermissions("mydy:movieimage:add")
    @Log(title = "影片图集", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Long movieId, MultipartFile file) throws IOException {
        DlMovieImage dlMovieImage = new DlMovieImage();
        dlMovieImage.setMovieId(movieId);
        //保存文件到相关路径
        //得到原始文件名
        String fileName = file.getOriginalFilename();
        //得到后缀名
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        //得到唯一文件名
        fileName = UUID.randomUUID()+suffix;
        //保存图片的路径
        File dirFile = new File(dirPath);
        //判断如果文件夹不存在 创建
        if (!dirFile.exists()){
            dirFile.mkdirs();
        }
        //得到文件的完整路径
        String filePath = dirPath+"/"+fileName;
        //保存文件  异常抛出
        file.transferTo(new File(filePath));
        //保存文件名
        dlMovieImage.setImagePath(fileName);
        return toAjax(dlMovieImageService.insertDlMovieImage(dlMovieImage));
    }

    /**
     * 修改影片图集
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        DlMovieImage dlMovieImage = dlMovieImageService.selectDlMovieImageById(id);
        mmap.put("dlMovieImage", dlMovieImage);
        return prefix + "/edit";
    }

    /**
     * 修改保存影片图集
     */
    @RequiresPermissions("mydy:movieimage:edit")
    @Log(title = "影片图集", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DlMovieImage dlMovieImage)
    {
        return toAjax(dlMovieImageService.updateDlMovieImage(dlMovieImage));
    }

    /**
     * 删除影片图集
     */
    @RequiresPermissions("mydy:movieimage:remove")
    @Log(title = "影片图集", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(dlMovieImageService.deleteDlMovieImageByIds(ids));
    }
}
